# -*- coding: utf-8 -*-
import mechanize
from mechanize import Browser
import re
import cStringIO as StringIO
import lxml.html
from lxml import etree
import json
from urllib import urlencode
import zlib
import datetime
import sys
import logging
from mongolog.handlers import MongoHandler
import cherrypy


# <!--- basic function --
def getCRC32Unsign(textToHash=None):
    return str(zlib.crc32(textToHash) & 0xffffffffL)

def buildTree(url):
    ua6300 = "Nokia6300/2.0 (04.20) Profile/MIDP-2.0 Configuration/CLDC-1.1 UNTRUSTED/1.0"
    br = Browser()
    br.addheaders = [("User-Agent", ua6300)]
    br.set_handle_equiv(True)
    br.set_handle_redirect(True)
    br.set_handle_referer(True)
    br.set_handle_robots(False)
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
    br.open(url, None, 600)
    html = br.response().read()
    parser = etree.HTMLParser(encoding='utf-8')
    tree = etree.parse(StringIO.StringIO(html), parser)
    return tree

def getElementText(elem):
    if elem is None: return ''
    t = lxml.html.fromstring(etree.tostring(elem))
    return t.text_content().strip()

def getAttributeText(node, attrb):
    if node is None or attrb == '': return ''
    return node.get(attrb)

def extractWithRegEx(pat, matchStr, matchIdx):
    ''' Hàm chạy regex và trả kết quả ở group[matchIdx]. Nếu không có kết quả -> trả về null'''
    try:
        result = ''
        rexp = re.compile(pat)
        m = rexp.search(matchStr)
        if (m!=''):
            result = m.group(matchIdx)
        return result
    except:
        return ''

def strToASCII(str):
    if str == '': return ''
    try:
        listPattern = [r"á|à|ả|ạ|ã|â|ấ|ầ|ẩ|ậ|ẫ|ă|ắ|ằ|ẳ|ặ|ẵ", r"Á|À|Ả|Ạ|Ã|Â|Ấ|Ầ|Ẩ|Ậ|Ẫ|Ă|Ắ|Ằ|Ẳ|Ặ|Ẵ",
                       r"đ", r"Đ", r"í|ì|ỉ|ị|ĩ", r"Í|Ì|Ỉ|Ị|Ĩ", r"é|è|ẻ|ẹ|ẽ|ê|ế|ề|ể|ệ|ễ", r"É|È|Ẻ|Ẹ|Ẽ|Ê|Ế|Ề|Ể|Ệ|Ễ",
                       r"ó|ò|ỏ|ọ|õ|ô|ố|ồ|ổ|ộ|ỗ|ơ|ớ|ờ|ở|ợ|ỡ", r"Ó|Ò|Ỏ|Ọ|Õ|Ô|Ố|Ồ|Ổ|Ộ|Ỗ|Ơ|Ớ|Ờ|Ở|Ợ|Ỡ",
                       r"ú|ù|ủ|ụ|ũ|ư|ứ|ừ|ử|ự|ữ", r"Ú|Ù|Ủ|Ụ|Ũ|Ư|Ứ|Ừ|Ử|Ự|Ữ", r"ý|ỳ|ỷ|ỵ|ỹ", r"Ý|Ỳ|Ỷ|Ỵ|Ỹ"]
        rep = ['a', 'A', 'd', 'D', 'i', 'I', 'e', 'E', 'o', 'O', 'u', 'U', 'y', 'Y']
        str = str.encode('utf-8', 'replace')
        for idx in range(len(listPattern)):
            str = re.sub(listPattern[idx], rep[idx], str)
        return str
    except:
        pass

# -- end basic function --!>

def compareURL(curURL, nextURL):
    curStart = extractWithRegEx("start=(\d+)", curURL, 1)
    nextStart = extractWithRegEx("start=(\d+)", nextURL, 1)
    print "\n\tcurstart {0} -- nextstart {1}".format(curStart, nextStart)
    if curStart == nextStart:   return True
    return False

def getLatLon(url):
    try:
        preg = re.compile(r"latlng=(\d+),(\d+)")
        m = preg.search(url)
        lat = str(float(m.group(1)) / 1000000.0)
        lng = str(float(m.group(2)) / 1000000.0)
        return {'lat': lat, 'lon': lng}
    except:
        return {'lat': '', 'lon': ''}
    
class GMap():
    
    @cherrypy.expose
    def index(self):
        return "Google map bridge"
        
    @cherrypy.expose
    def place(self, q, pg):
        if pg == '' or int(pg) < 1: pg = 1
        startRow = (int(pg) - 1) * 10
        url = "http://maps.google.com/m/placel?{0}&start={1}".format(urlencode({'q': q}), startRow)
        print url
        try:
            results = []
            tree = buildTree(url)
            if len(tree.xpath("//div[@class='bf']")) < 1:   return json.dumps({})
            for item in tree.xpath("//div[@class='bf']"):
                addr = getElementText(item.xpath(".//div[@class='k2e4se']")[0]).encode('utf-8', 'ignore')
                name = getElementText(item.xpath(".//a[@class='uf']")[0]).encode('utf-8', 'ignore')
                link = getAttributeText(item.xpath(".//a[@class='uf']")[0], 'href').encode('utf-8', 'ignore')
                latlon = getLatLon(link)
                tel = ''
                if len(item.xpath(".//a[@class='j4nalh']")) > 0:
                    tel = getElementText(item.xpath(".//a[@class='j4nalh']")[0]).encode('utf-8', 'ignore')
                results.append({'name': name, 'addr': addr, 'latlng': latlon, 'tel': tel})
#                print name
#                print addr
#                print latlon
#                print tel
#                print '----------------'
            return json.dumps(results)
        except:
            err = "{0} - {1} >> {2}".format(q, pg, sys.exc_info()[0])
            print err
            log.error(err)
            raise
        
        return json.dumps({})

if __name__ == '__main__':
    
    log = logging.getLogger('mapsgoogleBridge')
    log.setLevel(logging.DEBUG)
    log.addHandler(MongoHandler.to('mongolog', 'log'))
    
    global_conf = {
        'global': {
        'server.socket_host': '127.0.0.1',
        'server.socket_port': 8889,
        'tools.encode.on': True,
        'tools.encode.encoding': 'UTF-8'
        },
    }
    cherrypy.config.update(global_conf)
    cherrypy.quickstart(GMap())
    
    print "Finished at {0}".format(str(datetime.datetime.now()))
    sys.exit()


